package com.mall.db.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.utils.PageResult;
import com.mall.db.mapper.PmsProductAttributeCategoryMapper;
import com.mall.db.mapper.PmsProductAttributeMapper;
import com.mall.domain.PmsProductAttribute;
import com.mall.domain.PmsProductAttributeCategory;
import com.mall.interface1.api.PmsProductAttributeCategoryApi;
import com.mall.vo.PmsProductAttributeVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class PmsProductAttributeCategoryApiImpl implements PmsProductAttributeCategoryApi {

    @Autowired
    private PmsProductAttributeCategoryMapper categoryMapper;
    @Autowired
    private PmsProductAttributeMapper attributeMapper;

    //添加商品属性分类
    @Override
    public void create(String name) {
        PmsProductAttributeCategory category=new PmsProductAttributeCategory();
        category.setName(name);
        categoryMapper.insert(category);
    }

    //获取所有商品属性分类及其下属性
    @Override
    public List<PmsProductAttributeVo> listWithAttr() {
        List<PmsProductAttributeVo> productAttributeVoList = new ArrayList<>();
        LambdaQueryWrapper<PmsProductAttributeCategory> wrapper = new LambdaQueryWrapper<>();
        List<PmsProductAttributeCategory> attributeCategoryMapperAll = categoryMapper.selectList(wrapper);
        for (PmsProductAttributeCategory productAttributeCategory : attributeCategoryMapperAll) {
            PmsProductAttributeVo productAttributeVo = new PmsProductAttributeVo();
            BeanUtils.copyProperties(productAttributeCategory,productAttributeVo);
            LambdaQueryWrapper<PmsProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PmsProductAttribute::getProductAttributeCategoryId,productAttributeCategory.getId());
            List<PmsProductAttribute> productAttributeList = attributeMapper.selectList(queryWrapper);
            productAttributeVo.setProductAttributeList(productAttributeList);
            productAttributeVoList.add(productAttributeVo);
        }
        return productAttributeVoList;
    }

    //分页获取所有商品属性分类
    @Override
    public Page<PmsProductAttributeCategory> listAttribute(Integer pageNum, Integer pageSize) {
        Page<PmsProductAttributeCategory> pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<PmsProductAttributeCategory> wrapper = new LambdaQueryWrapper<>();
        Page<PmsProductAttributeCategory> page = categoryMapper.selectPage(pageInfo, wrapper);
        return page;
    }

    //删除单个商品属性分类
    @Override
    public void deleteById(Long id) {
        categoryMapper.deleteById(id);
    }

    //修改商品属性分类
    @Override
    public void updateById(Long id, String name) {
        PmsProductAttributeCategory productAttributeCategory = new PmsProductAttributeCategory();
        productAttributeCategory.setName(name);
        LambdaQueryWrapper<PmsProductAttributeCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductAttributeCategory::getId,id);
        categoryMapper.update(productAttributeCategory,wrapper);
    }

    //获取单个商品属性分类信息
    @Override
    public PmsProductAttributeCategory attributeCategoryById(Long id) {
        LambdaQueryWrapper<PmsProductAttributeCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductAttributeCategory::getId,id);
        PmsProductAttributeCategory productAttributeCategory = categoryMapper.selectOne(wrapper);
        return productAttributeCategory;
    }


}
